package paa.simulator.grafo;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;

import paa.simulator.grafo.PessoaPool.Pessoa;
import paa.simulator.grafo.VideoPool.Video;


public class PossibleRelations {

	
	private Map<Video, Set<Pessoa>> possibleVideoRelations;
	private Map<Integer, Set<Pessoa>> possibleTagRelations;
	private Map<Pessoa, Set<Pessoa>> possibleSubscripionsRelations;
	
	public PossibleRelations() {
		this.possibleVideoRelations = new WeakHashMap<Video, Set<Pessoa>>();
		this.possibleTagRelations = new WeakHashMap<Integer, Set<Pessoa>>();
		this.possibleSubscripionsRelations = new WeakHashMap<Pessoa, Set<Pessoa>>();
	}
	
	public void addPossibleVideoRelation(Video video, Pessoa pessoa) {
		Set<Pessoa> relations = getPossibleVideoRelations(video);
		if (relations == null) {
			relations = new HashSet<Pessoa>();
			possibleVideoRelations.put(video, relations);
		}
		
		relations.add(pessoa);
	}

	public void removePossibleVideoRelation(Video video, Pessoa pessoa) {
		Set<Pessoa> relations = getPossibleVideoRelations(video);
		if (relations != null) {
			relations.remove(pessoa);
			
			if (relations.isEmpty()) {
				removePossibleVideoRelations(video);
			}
		}
	}
	
	public void addPossibleTagRelation(Integer tag, Pessoa pessoa) {
		Set<Pessoa> relations = getPossibleTagRelations(tag);
		if (relations == null) {
			relations = new HashSet<Pessoa>();
			possibleTagRelations.put(tag, relations);
		}
		
		relations.add(pessoa);
	}
	
	public void removePossibleTagRelation(Integer tag, Pessoa pessoa) {
		Set<Pessoa> relations = getPossibleTagRelations(tag);
		if (relations != null) {
			relations.remove(pessoa);
			
			if (relations.isEmpty()) {
				removePossibleTagRelations(tag);
			}	
		}
	}
	
	public void addPossibleSubscriptionRelation(Pessoa subscription, Pessoa pessoa) {
		Set<Pessoa> relations = getPossibleSubscriptionRelations(subscription);
		if (relations == null) {
			relations = new HashSet<Pessoa>();
			possibleSubscripionsRelations.put(subscription, relations);
		}
		
		relations.add(pessoa);
	}
	
	public void removePossibleSubscriptionRelation(Pessoa subscription, Pessoa pessoa) {
		Set<Pessoa> relations = getPossibleSubscriptionRelations(subscription);
		if (relations != null) {
			relations.remove(pessoa);
			if (relations.isEmpty()) {
				removePossibleSubscriptionRelations(subscription);
			}
		}
	}
	
	public Set<Pessoa> getPossibleVideoRelations(Video video) {
		return this.possibleVideoRelations.get(video);	
	}
	
	public Set<Pessoa> getPossibleTagRelations(Integer tag) {
		return this.possibleTagRelations.get(tag);
	}

	public Set<Pessoa> getPossibleSubscriptionRelations(Pessoa subscription) {
		return this.possibleSubscripionsRelations.get(subscription);
	}
	
	public Set<Pessoa> removePossibleVideoRelations(Video video) {
		return this.possibleVideoRelations.remove(video);
	}
	
	public Set<Pessoa> removePossibleTagRelations(Integer tag) {
		return this.possibleTagRelations.remove(tag);
	}
	
	public Set<Pessoa> removePossibleSubscriptionRelations(Pessoa subscription) {
		return this.possibleSubscripionsRelations.remove(subscription);
	}
	
	public void clear() {
		this.possibleTagRelations.clear();
		this.possibleVideoRelations.clear();
		this.possibleSubscripionsRelations.clear();
	}
}
